package com.qs.eggs.account;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.R;
import com.qs.eggs.api.HttpApi;
import com.qs.eggs.base.activity.BaseActivity;
import com.qs.eggs.refresh.utils.TDevice;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import okhttp3.Call;
import okhttp3.Response;

public class RegisterStepOneActivity extends BaseActivity {

    private EditText mPhone,mSmsCode;
    private TextView tvCode,tvNext;
    private CountDownTimer mTimer;
    String CodeS="";
    String phoneNumber;
    public static Activity instance;
    private StringCallback mHandler=new StringCallback() {
        @Override
        public void onSuccess(String s, Call call, Response response) {

            CodeS=s.replace("\"","");
        }

        @Override
        public void onError(Call call, Response response, Exception e) {
            super.onError(call, response, e);
            if (mTimer != null) {
                mTimer.onFinish();
                mTimer.cancel();
            }
        }
    };
    @Override
    protected int getContentView() {
        return R.layout.activity_register_step_one;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        instance=this;
        mPhone=(EditText)findViewById(R.id.et_phone);
        mSmsCode=(EditText)findViewById(R.id.et_register_auth_code);
        tvCode=(TextView) findViewById(R.id.tv_register_sms_call);
        tvNext=(TextView) findViewById(R.id.tv_next);

        tvNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(mSmsCode.length()==0){
                   Toast.makeText(RegisterStepOneActivity.this,"验证码不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(!mSmsCode.getText().toString().trim().equals(CodeS)){
                    Toast.makeText(RegisterStepOneActivity.this,"验证码填写不正确",Toast.LENGTH_SHORT).show();
                    return;
                }

                Intent intent=new Intent(RegisterStepOneActivity.this,RegisterStepTwoActivity.class);
                intent.putExtra("code",CodeS);
                intent.putExtra("phone",phoneNumber);
                startActivity(intent);
            }
        });

        tvCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mPhone.length()==0){
                    Toast.makeText(RegisterStepOneActivity.this,"请输入手机号",Toast.LENGTH_SHORT).show();
                    return;
                }
                phoneNumber = mPhone.getText().toString().trim();

                if(!isMobile(phoneNumber)){
                    Toast.makeText(RegisterStepOneActivity.this,"输入手机号格式不正确",Toast.LENGTH_SHORT).show();
                    return;
                }
                HttpApi.cheakPhone(phoneNumber, new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {

                        if(s.equals("\"true\"")){
                            requestSmsCode();
                        }else {
                            Toast.makeText(RegisterStepOneActivity.this,"该号码已注册",Toast.LENGTH_SHORT).show();
                        }

                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                    }
                });

            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
    }



    private void requestSmsCode() {
//        if (!mMachPhoneNum) {
//            //showToastForKeyBord(R.string.hint_username_ok);
//            return;
//        }
        if (!TDevice.hasInternet()) {
            Toast.makeText(RegisterStepOneActivity.this,R.string.tip_network_error,Toast.LENGTH_SHORT).show();
            return;
        }

        if (tvCode.getTag() == null) {
        //    mRequestType = 1;
            tvCode.setTag(true);
            mTimer = new CountDownTimer(60 * 1000, 1000) {

                @SuppressLint("DefaultLocale")
                @Override
                public void onTick(long millisUntilFinished) {
                    tvCode.setText(String.format("%s%s%d%s",
                            "等待", "(", millisUntilFinished / 1000, ")"));
                }

                @Override
                public void onFinish() {
                    tvCode.setTag(null);
                    tvCode.setText("发送验证码");
                }
            }.start();

            HttpApi.sendSmsCode(phoneNumber, mHandler);
        } else {
            Toast.makeText(RegisterStepOneActivity.this,"请稍后重试", Toast.LENGTH_SHORT).show();
        }
    }

    //判断手机格式是否正确
    public boolean isMobile(String mobiles) {

        Pattern p = Pattern.compile("1(3|4|5|7|8)[0-9]{9}");
        Matcher m = p.matcher(mobiles);
        return m.matches();

    }
}
